package lily.phs.entertain;

import lily.phs.urh.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

public class MediaListActivity extends Activity {
    /** Called when the activity is first created. */
	private ListView listview;
	private int[] _ids;
	private String[]_titles;
	private ScanSdReceiver scanSdReceiver = null;
	private AlertDialog ad = null;
	private AlertDialog.Builder  builder = null;
	
//	TextView tv_TitleBar;
//	private GridView toolbarGrid;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //scanSdCard();
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		this.setContentView(R.layout.media_main);
		
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.i_title);
		
//		tv_TitleBar = (TextView)this.findViewById(R.id.tv_titlebar);
//        tv_TitleBar.setText("全部音乐");
		Button btBack = (Button)this.findViewById(R.id.title_left_btn);
		btBack.setText("后退");
		Button btSave = (Button)this.findViewById(R.id.title_right_btn);
		btSave.setVisibility(View.GONE);
		Button btSubmit = (Button)this.findViewById(R.id.title_right_btn2);
		btSubmit.setVisibility(View.GONE);
		TextView tvTitle = (TextView)this.findViewById(R.id.title);
		tvTitle.setText("全部音乐");
		
		btBack.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
				overridePendingTransition(R.anim.anim_slide_in_left,R.anim.anim_slide_out_right);
			}
		});
		
        listview = (ListView)findViewById(R.id.media_main_List);
        Cursor c = this.getContentResolver()
        		.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
        				new String[]{MediaStore.Audio.Media.TITLE,
						MediaStore.Audio.Media.DURATION,
						MediaStore.Audio.Media.ARTIST,
						MediaStore.Audio.Media._ID,
						MediaStore.Audio.Media.DISPLAY_NAME },
        				null, null, null);
        if (c==null || c.getCount()==0){
        	builder = new AlertDialog.Builder(this);
			builder.setMessage("存储列表为空...").setPositiveButton("确定", null);
			ad = builder.create();
			ad.show();
        }
        c.moveToFirst();
        _ids = new int[c.getCount()];
        _titles = new String[c.getCount()];
        for(int i=0;i<c.getCount();i++){
        	_ids[i] = c.getInt(3);
        	_titles[i] = c.getString(0);
        	c.moveToNext();
        }
        listview.setAdapter(new MusicListAdapter(this, c));
        listview.setOnItemClickListener(new ListItemClickListener());
               
    }
   
    class ListItemClickListener implements OnItemClickListener{

    	@Override
    	public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
    		// TODO Auto-generated method stub
    		Intent intent = new Intent(MediaListActivity.this,MusicActivity.class);
    		intent.putExtra("_ids", _ids);
    		intent.putExtra("_titles", _titles);
    		intent.putExtra("position", position);
    		startActivity(intent);
    		finish();
    	}
    	
    }
    
   
    
    public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (scanSdReceiver!=null)
      				unregisterReceiver(scanSdReceiver);
			Intent intent = new Intent();
			intent.setClass(this, MediaMainActivity.class);
			startActivity(intent);
			finish();
		}
		return true;
    }

}

